/*
 * Copyright (c) 2017. Visionet and/or its affiliates. All right reserved.
 * VISIONET PROPRIETARY/CONFIDENTIAL.
 */
package com.visionet.jumper.project.sample.controller.security;

import com.visionet.jumper.core.common.Message;
import com.visionet.security.domain.dto.LoginUserDTO;
import com.visionet.security.domain.dto.UserDTO;
import com.visionet.security.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author TC.Ubuntu
 * @since 2017/7/4.
 */
@Controller
@RequestMapping("/security")
public class SecurityController {

	private Logger logger = LoggerFactory.getLogger(SecurityController.class);

	@Resource
	private UserService userService;

	@RequestMapping("/index")
	public String index() {
		return "system/login";
	}

	/**
	 * 用户登录
	 * @param user
	 * @param modelAndView
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(LoginUserDTO user, ModelAndView modelAndView, HttpServletRequest httpServletRequest) {
		Message loginMessage;
		UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginName(),user.getPassword());
		token.setRememberMe(user.getRememberMe());
		Subject subject = SecurityUtils.getSubject();
		try{
			subject.login(token);
//			String requestUrl = WebUtils.getSavedRequest(httpServletRequest).getRequestURI();
			modelAndView = new ModelAndView("redirect: "+ httpServletRequest.getContextPath()+ "/index");
			return modelAndView;
		}catch(IncorrectCredentialsException | UnknownAccountException e){
			logger.warn(e.getMessage(), e);
			loginMessage = new Message("failed", "用户名/密码错误");
		}catch(AccountException e){
			loginMessage = new Message("failed", "该用户不可用");
		}catch(AuthenticationException ae){
			logger.info("认证失败："+ae.getMessage());
			loginMessage = new Message("failed", "认证失败");
		}
		modelAndView.setViewName("system/login");
		modelAndView.addObject("message", loginMessage);
		modelAndView.addObject("loginName", user.getLoginName());
		return modelAndView;
	}

	/**
	 * 用户登出
	 * @param httpServletRequest
	 * @return
	 */
	@RequestMapping("/logout")
	public String login(HttpServletRequest httpServletRequest) {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		return "redirect: "+ httpServletRequest.getContextPath()+ "/security/index";
	}

	@RequestMapping(value = "/register")
	public String register() {
		return "system/userRegister";
	}

	@RequestMapping(value = "/userRegister", method = RequestMethod.POST)
	public String user(UserDTO registerUser, HttpServletRequest httpServletRequest) {
		try{
			userService.addUser(registerUser);
		} catch(Exception e){
			logger.info(e.getMessage());
		}
		return "redirect: "+ httpServletRequest.getContextPath()+ "/security/index";
	}
}
